package com.cn.controller;

import com.cn.entity.User;
import com.cn.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * @author: chenyuncong
 * @Date: 2019/12/5 9:25
 */
@RestController
@RequestMapping("user2")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    String ip=null;
    public UserController(){
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getUserById")
    public User getUserById(Integer id){
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                log.info("name:[{}] valuse:[{}]",name,values);
            }
        }
        User u = userService.getUserById(id);
        u.setCIp(ip);
        return u;
    }
}
